

package Database;

import java.net.ConnectException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class playgame {
    public static int numberrow(int type)
    {
        int kq=0;
        try {
            PreparedStatement st=Connect.Connect.Con().prepareStatement("select count(PuzzleID) from Puzzle where type=?");
            st.setInt(1, type);
            ResultSet set=st.executeQuery();
            if(set.next())
            {
                kq=set.getInt(1);
            }
            set.close();
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(playgame.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kq;
    }
    public static ResultSet getgame(int type)
    {
        ResultSet set=null;
        int count=numberrow(type);
        int posion=Pro.random.laygiatri(count);
        int game_id=get_id(posion, type);
        try {
            PreparedStatement st=Connect.Connect.Con().prepareStatement("select PuzzleID, [Name], account, Puzzle.[type] from Puzzle, Admin where Admin.AdminID=Puzzle.AdminID and PuzzleID=?");
            st.setInt(1, game_id);
            set=st.executeQuery();
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(playgame.class.getName()).log(Level.SEVERE, null, ex);
        }

        return set;
    }
    //De tao ra ramdom cau hoi, ham nay dung de lay cau hoi o vi tri random da tao ra
    public static int get_id(int posion, int type)
    {
        //posion--;
        int kq=0;
        try {
            ResultSet set=Connect.Connect.Con().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery("select * from Puzzle where type="+type);
            if(set.absolute(posion))
            {
                kq=set.getInt(1);
            }
            set.close();
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(playgame.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kq;
    }
    public static Object[][] getpuzzlecell_que(int puid, int type)
    {
        Object[][] kq=new Object[type][type];
        PreparedStatement st;
        try {
            st = Connect.Connect.Con().prepareStatement("select * from Pu_Question where PuzzleID=?");
            st.setInt(1, puid);
            ResultSet set=st.executeQuery();
            for (int i = 0; i < type; i++) {
                for (int j = 0; j < type; j++) {
                    kq[i][j]="*";
                }
            }
            int index=0;
            while(set.next())
            {
                String temp=set.getString(6);
                int qtype=0;
                if(set.getString(3).equals("1"))
                {
                    qtype=1;
                }
                String[] op=temp.split("-");
                int[] tam=Pro.convert.number_to_table(new Integer(op[0]), type);
                index++;
                kq[tam[0]][tam[1]]=index+"";
                for (int i = 1; i < op.length; i++) {
                    int[] tam2=Pro.convert.number_to_table(new Integer(op[i]), type);
                    kq[tam2[0]][tam2[1]]="";
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(playgame.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kq;
    }
    public static void save_player_game(String username, int Puid, int time_second, String opfini, String status, int mask)
    {
        //insert
        insert_game(username, Puid, time_second, opfini, mask, status);
    }
   
    public static void insert_game(String usernam, int Puid, int second, String opfi, int mark, String status)
    {
        try {
            PreparedStatement st=Connect.Connect.Con().prepareStatement("insert into Player_Infor values(?,?,?,?,?,?)");
            st.setString(1, usernam);
            st.setInt(2, Puid);
            st.setInt(3, second);
            st.setString(4, opfi);
            st.setInt(5, mark);
            st.setString(6, status);
            st.executeUpdate();
            st.close();
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(playgame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    public static ResultSet get_highsc()
    {
        ResultSet set=null;
        try {
            PreparedStatement st = Connect.Connect.Con().prepareStatement("select top 10 Username, Total_Mark from Player_Infor order by Total_Mark desc");
            set=st.executeQuery();
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(playgame.class.getName()).log(Level.SEVERE, null, ex);
        }
        return set;
    }
}
